﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.ComponentModel;
using System.Windows.Forms;
using System.IO;

namespace BDSim
{
    /// <summary>
    /// 天体运动模型
    /// </summary>
    public class AstroMotionModel : AstronomyEnvModel
    {
        #region 字段和属性
        /// <summary>
        /// 右键命令集合
        /// </summary>
        private List<MenuCommand> MenuCommandList = new List<MenuCommand>();
        /// <summary>
        /// 文件参数名
        /// </summary>
        private string _FileName = currentDirectory + "\\ConfigFiles\\EPHDAT.txt";
        [XmlAttribute, ReadOnly(true), CategoryAttribute("模型设置")]
        public string FileName
        {
            get { return _FileName; }
            set { _FileName = value; }
        }

        #endregion

        #region 构造函数
        /// <summary>
        /// 构造函数
        /// </summary>
        public AstroMotionModel()
        {
            MenuCommand LoadFile = new MenuCommand();
            LoadFile.Header = "加载行星历表文件";
            LoadFile.DefineExcuteCommand(ShowLoadFileDialog_Excute, ShowLoadFileDialog_CanExcute);
            MenuCommandList.Add(LoadFile);

            MenuCommand CalSunMoonPosCmd = new MenuCommand();
            CalSunMoonPosCmd.Header = "计算日月位置";
            CalSunMoonPosCmd.DefineExcuteCommand(ShowCalSunMoonDialog);
            MenuCommandList.Add(CalSunMoonPosCmd);

            Menu.ItemsSource = MenuCommandList;
        }

        #endregion

        #region 方法
        /// <summary>
        /// 显示加载文件对话框
        /// </summary>
        /// <param name="param"></param>
        private void ShowLoadFileDialog_Excute(object param)
        {
            LoadAstronomyEnvFile loadFile = new LoadAstronomyEnvFile(1);
            loadFile.FileName = this.FileName;
            loadFile.ShowDialog();
            if (loadFile.SelResult == true)
            {
                this.FileName = loadFile.FileName;
                //add by liuy on 20160803
                ReadEphdatFile();
            }
        }
        private bool ShowLoadFileDialog_CanExcute(object param)
        {
            bool flag = false;
            if (BDSimObject.GlobalStatus == ESimStatus.UnInited || BDSimObject.GlobalStatus == ESimStatus.End)
            {
                flag = true;
            }
            return flag;
        }

        /// <summary>
        /// 显示计算日月位置对话框
        /// </summary>
        private void ShowCalSunMoonDialog(object param)
        {
            CalSunMoonPos csmp = new CalSunMoonPos();
            csmp.ShowDialog();
        }

        /// <summary>
        /// 计算日月位置功能函数
        /// </summary>
        public static bool CalSunMoonPos(UTCTime time, double[] PosMoon, double[] PosSun)
        {
            BDSim.TimeSpaceDll.CAL2UTC(out time, 1);
            int leapSec = BDSim.ReadEOP.EOP_GetLeapSec(time) + 19; //GPST_UTC + 19

            //计算日月位置实例
            double TAI = time.JD + (leapSec / 86400.0);
            double EphemerisT = TAI + 0.0003725;
            int PlaneFlag = 3;
            int NutationFlag = 0;
            double[] Nutations = new double[6];
            double[] Librations = new double[6];

            int a = BDSim.PlanetEphDLL.PL_POSITION(EphemerisT, PlaneFlag, NutationFlag, Nutations, Librations, PosMoon, PosSun);

            if (a < 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }

        /// <summary>
        /// 初始化函数
        /// </summary>
        public override bool Init()
        {
            return ReadEphdatFile();
        }

        /// <summary>
        /// 读行星历表文件
        /// </summary>
        /// <returns></returns>
        unsafe private bool ReadEphdatFile()
        {
            UTCTime start = BDSimObject.GlobalStartTime;
            UTCTime end = BDSimObject.GlobalEndTime;
            
            bool ret = BDSim.ReadEphdat.RE_Init(FileName.ToCharArray(), start, end);
            if (ret == true)
            {
                int row = 0;
                int col = 0;
                double* pdata = BDSim.ReadEphdat.RE_GetEphdat(out row, out col);
                //计算日月位置动态库初始化
                BDSim.PlanetEphDLL.PL_Init(pdata, row, col);
            }
            else
            {
                MessageBox.Show("读行星历表文件失败！", "错误");               
                return false;
            }

            return false;
        }

        #endregion
    }
}
